package com.dxj1992.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxj1992.entity.AdminRoleAdmins;
import com.dxj1992.entity.AdminRoles;
import com.dxj1992.entity.AdminUsers;
import com.dxj1992.mapper.AdminRolesMapper;
import com.dxj1992.mapper.AdminUsersMapper;
import com.dxj1992.service.IAdminRoleAdminsService;
import com.dxj1992.service.IAdminUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-03-29
 */
@Service
public class AdminUsersServiceImpl extends ServiceImpl<AdminUsersMapper, AdminUsers> implements IAdminUsersService {

    @Autowired
    private AdminUsersMapper adminUsersMapper;
    @Autowired
    private AdminRolesMapper adminRolesMapper;
    @Autowired
    private IAdminRoleAdminsService iAdminRoleAdminsService;

    @Override
    public List<AdminUsers> allocatedList(AdminUsers adminUsers) {

        List<AdminUsers> list = adminUsersMapper.selectAllocatedList(adminUsers);
        return list;
    }

    @Override
    public List<AdminUsers> selectUnallocatedList(AdminUsers adminUsers) {
        List<AdminUsers> list = adminUsersMapper.selectUnallocatedList(adminUsers);
        return list;
    }

    @Override
    public Boolean selectAll(String userIds, AdminUsers adminUsers) {

        String[] split = userIds.split(",");
        List<String> ids = Arrays.asList(split);

        List<AdminRoleAdmins> list = new ArrayList<>();
        for (String id : ids) {
            LambdaQueryWrapper<AdminRoleAdmins> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AdminRoleAdmins::getAdminRoleId, adminUsers.getRoleId());
            queryWrapper.eq(AdminRoleAdmins::getAdminUserId, Integer.valueOf(id));
            AdminRoleAdmins adminRolePermissions = iAdminRoleAdminsService.getOne(queryWrapper);
            if (!Objects.isNull(adminRolePermissions)) {
                continue;
            }
            list.add(new AdminRoleAdmins(adminUsers.getRoleId(), Integer.valueOf(id)));
        }
        boolean b = iAdminRoleAdminsService.saveBatch(list, 50);
        if (!b) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean cancelAll(String userIds, AdminUsers adminUsers) {
        String[] split = userIds.split(",");
        List<String> ids = Arrays.asList(split);
        for (String id : ids) {
            LambdaQueryWrapper<AdminRoleAdmins> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AdminRoleAdmins::getAdminRoleId, adminUsers.getRoleId());
            queryWrapper.eq(AdminRoleAdmins::getAdminUserId, Integer.valueOf(id));
            AdminRoleAdmins adminRolePermissions = iAdminRoleAdminsService.getOne(queryWrapper);
            if (!Objects.isNull(adminRolePermissions)) {
                iAdminRoleAdminsService.remove(Wrappers.<AdminRoleAdmins>lambdaQuery()
                        .eq(AdminRoleAdmins::getAdminRoleId, adminRolePermissions.getAdminRoleId())
                        .eq(AdminRoleAdmins::getAdminUserId, adminRolePermissions.getAdminUserId()));
            }
        }
        return true;
    }

    @Override
    public Boolean removeData(String ids) {
        String[] split = ids.split(",");
        List<String> idList = Arrays.asList(split);
        for (String id : idList) {
            int count = iAdminRoleAdminsService.count(Wrappers.<AdminRoleAdmins>lambdaQuery().eq(AdminRoleAdmins::getAdminRoleId, id));
            if (count > 0) {
                throw new RuntimeException("当前下面已经分配数据不能删除");
            }

            QueryWrapper<AdminRoles> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", id);
            int row = adminRolesMapper.delete(queryWrapper);
            if (row == 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean insertUserAuth(AdminUsers adminUsers) {
        boolean remove = iAdminRoleAdminsService.remove(Wrappers.<AdminRoleAdmins>lambdaQuery().eq(AdminRoleAdmins::getAdminUserId, adminUsers.getId()));

        Long[] roleIds = adminUsers.getRoleIds();
        for (Long roleId : roleIds) {
            AdminRoleAdmins adminRoleAdmins = new AdminRoleAdmins();
            adminRoleAdmins.setAdminRoleId(roleId.intValue());
            adminRoleAdmins.setAdminUserId(adminUsers.getId());
            iAdminRoleAdminsService.save(adminRoleAdmins);
        }
        return true;
    }
}
